<?php
namespace Home\Controller;


class IndexController extends BaseController {

    public function index()
    {
        //轮播图为手机端和电脑端共有的数据
        $banners = D('Banner')->where(array('is_show'=>array('eq',1)))->order('sort asc')->limit(3)->select();

        $news = D('News')->order('pub_time desc')->limit(5)->select();

        if ($this->isMobile()) {
            $friends = D('Friend')->order('sort asc')->limit(6)->select();

            //对标题长度进行处理
            foreach ($news as $key => $new) {
                $news[$key]['title'] = msubstr($new['title'], 0, 15);
            }

            $this->assign(array(
                'friends' => $friends,
                'banners' => $banners,
                'news'  => $news
            ));
            $this->display('mobile/Index/index');
        } else {
            $friends = D('Friend')->order('sort asc')->limit(5)->select();
            //因为标题长度不超过30个字,电脑端不用处理.
            $this->assign(array(
                'friends'  => $friends,
                'news'     => $news,
                'banners'  => $banners
            ));
            $this->display('web/Index/index');
        }
    }


    public function search()
    {
        $keyword = I('get.keyword');
        $New = D('News');
        if (!empty($keyword)) {
            $where['title']=array('like','%'.$keyword.'%');
        }
        $news = $New->where($where)->order('pub_time desc')->select();

        foreach ($news as $key => $new) {
            $news[$key]['content'] = msubstr(strip_tags(htmlspecialchars_decode($new['content'])), 0, 100);
        }

        $count=$New->where($where)->count();
        $page=new \Think\Page($count,10);

        $page->setConfig('prev', '上一页');
        $page->setConfig('next', '下一页');
        $page->setConfig('last', '末页');
        $page->setConfig('first', '首页');

        $page->lastSuffix = false;//最后一页不显示为总页数
        $show=$page->show();
        $this->assign(array(
            "news"     => $news,
            'keyword'  => $keyword,
            'count'    => $count,
            "show"     => $show
        ));
        $this->display('web/Index/search');
    }
}